package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysChangeLog;
import com.ruoyi.system.service.ISysChangeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 更新日志Controller
 *
 * @author siashan
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/system/changelog")
public class SysChangeLogController extends BaseController {

    @Autowired
    private ISysChangeLogService sysChangeLogService;

    /**
     * 查询更新日志列表
     *
     * @param sysChangeLog 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysChangeLog sysChangeLog) {
        startPage();
        LambdaQueryWrapper<SysChangeLog> lqw = new LambdaQueryWrapper<SysChangeLog>();
        if (StringUtils.isNotBlank(sysChangeLog.getVersionNum())) {
            lqw.eq(SysChangeLog::getVersionNum, sysChangeLog.getVersionNum());
        }
        List<SysChangeLog> list = sysChangeLogService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出更新日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:export')")
    @Log(title = "更新日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysChangeLog sysChangeLog) {
        LambdaQueryWrapper<SysChangeLog> lqw = new LambdaQueryWrapper<SysChangeLog>();
        if (StringUtils.isNotBlank(sysChangeLog.getVersionNum())) {
            lqw.eq(SysChangeLog::getVersionNum, sysChangeLog.getVersionNum());
        }
        if (sysChangeLog.getReleaseTime() != null) {
            lqw.eq(SysChangeLog::getReleaseTime, sysChangeLog.getReleaseTime());
        }
        if (StringUtils.isNotBlank(sysChangeLog.getContent())) {
            lqw.eq(SysChangeLog::getContent, sysChangeLog.getContent());
        }
        List<SysChangeLog> list = sysChangeLogService.list(lqw);
        ExcelUtil<SysChangeLog> util = new ExcelUtil<SysChangeLog>(SysChangeLog.class);
        util.exportExcel(response, list, "更新日志数据");
    }

    /**
     * 获取更新日志详细信息
     *
     * @param id id
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysChangeLogService.getById(id));
    }

    /**
     * 新增更新日志
     *
     * @param sysChangeLog 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:add')")
    @Log(title = "更新日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysChangeLog sysChangeLog) {
        return toAjax(sysChangeLogService.save(sysChangeLog) ? 1 : 0);
    }

    /**
     * 修改更新日志
     *
     * @param sysChangeLog 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:edit')")
    @Log(title = "更新日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysChangeLog sysChangeLog) {
        return toAjax(sysChangeLogService.updateById(sysChangeLog) ? 1 : 0);
    }

    /**
     * 删除更新日志
     *
     * @param ids 主键id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:changelog:remove')")
    @Log(title = "更新日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysChangeLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
